//
//  DZVisitorView.swift
//  Swift微博
//
//  Created by 董筱泽 on 16/10/23.
//  Copyright © 2016年 董筱泽. All rights reserved.
//

import UIKit

class DZVisitorView: UIView {

    // 01-监听访客视图 view 点击回调
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 供外界调用方法
    /*
     - 因为当前访客视图是参照首页开发的 没有必要再设置
     - 使用可选值 为了开发辨别是要修改哪个界面的HMVisitorView icon 和 messageLabel
     */
    func setupVisitorViewInfo(imgName: String?, tilte: String?){
        // 判断imgName&tilte 是否为 nil
        // 代表就是(消息 发现 我)
        if let img = imgName, let tit = tilte{
            // 设置 image 和 text
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            // 隐藏圆圈
            circleImageView.isHidden = true
        }else {
            // 代表是首页 不需要在设置了
            setupCircleImageViewAnim()
        }
    }
    // 设置圆圈动画
    private func setupCircleImageViewAnim(){
        // 核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置转的角度
        anim.toValue = M_PI * 2
        // 设置时间
        anim.duration = 20
        // 重复次数
        anim.repeatCount = MAXFLOAT
        // 完成以后是否移除动画
        /*
         - 如果你切换控制器 或者程序推到后台 默认情况下动画会被移除
         - 解决办法 isRemovedOnCompletion = false
         */
        anim.isRemovedOnCompletion = false
        // 添加动画
        circleImageView.layer.add(anim, forKey: nil)
    }
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 04文字
        messageLabel.snp_makeConstraints { (make) in
            make.width.equalTo(230)
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
        }
        
        // 05注册按钮
        registerButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        
        // 07登录按钮
        loginButton.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }

    }
    // 圆圈
    fileprivate lazy var circleImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    // 遮罩
    fileprivate lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    // icon
    fileprivate lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    // 注册按钮
    fileprivate lazy var registerButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "注册", fontSize: 14, titleColor: UIColor.black, target: self, action: #selector(loginClick))
        return button
    }()
    // 文字
    fileprivate lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.darkGray, fontSize: 14, text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
        lab.textAlignment = .center
        // 换行
        lab.numberOfLines = 0
        return lab
    }()

    // 登录按钮
    fileprivate lazy var loginButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "登录", fontSize: 14, titleColor: UIColor.black, target: self, action: #selector(loginClick))
        return button
    }()

    @objc private func loginClick(){
        // 03- 执行闭包
        closure?()
    }
}
